﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using System.Web;

namespace WebBrowserTest.Utility
{
    public class Utils
    {
        public static string GetUrlFromConfiguration(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        public static string getPageHtmlString(string url)
        {
            
            WebClient webClient = new WebClient();
            StringBuilder htmlContentBuilder = null;
            try
            {
                //webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0");
                //webClient.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                //webClient.Headers.Add("Accept-Language", "pl,en-us;q=0.7,en;q=0.3");
                //webClient.Headers.Add("Accept-Charset", "ISO-8859-2,utf-8;q=0.7,*;q=0.7");

                htmlContentBuilder = new StringBuilder(webClient.DownloadString(url));
            }
            catch (Exception)
            {
                return string.Empty;
            }
            string pageContent = htmlContentBuilder.Replace("\t", "").Replace("\n","").Replace("\r","").ToString();
            var matches = Regex.Matches(pageContent, "charset=([0-9a-zA-Z-]+)");
            if (matches.Count > 0)
            {
                string encoding = matches[0].Groups[1].Value;
                Encoding pageEnc = Encoding.UTF8;
                if (!string.IsNullOrEmpty(encoding))
                {
                    try
                    {
                        pageEnc = Encoding.GetEncoding(encoding);
                    }
                    catch (ArgumentException)
                    {

                        pageEnc = Encoding.Default;
                    }
                }
                pageContent = pageEnc.GetString(Encoding.Default.GetBytes(pageContent));
            }
            return pageContent;
        }

        public static string ExtractDomainNameFromUR(string Url)
        {
            return Regex.Replace(
                Url,
                @"^([a-zA-Z]+:\/\/)?([^\/]+)\/.*?$",
                "$2"
            );
        }
    }

}
